package com.service.impl;

import com.bean.Users;
import com.bean.vo.UserVo;
import com.mapper.UsersMapper;
import com.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.jaas.AuthorityGranter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
//安全认证的SecuriyService
@Service
public class MyUserSecuriyService implements UserDetailsService {


    @Autowired
    private UsersMapper usersMapper;


    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
       Users u=new  Users();
       u.setUserName(userName);
        UserVo user=null;
       if(StringUtils.isNotEmpty(usersMapper.select(u))){
           user=usersMapper.select(u).get(0);
       }else{
           throw  new UsernameNotFoundException("用户名不存在");
       }
       //获取认证权限
        List<GrantedAuthority>authorities=new ArrayList<>();
//       List<String>roles=new ArrayList<>();
//        roles.add(user.getRoleName());
       //获取大昂起按用户集合
        GrantedAuthority g=new SimpleGrantedAuthority(user.getRoleName());
        authorities.add(g);

        User su=new User(user.getUserName(),user.getPassword(),authorities);
        return su;
    }



}
